package com.sundablog.service.impl.index;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sundablog.mapper.AdminPermissionMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.mapper.AdminUserRoleMapper;
import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminPermissionExample;
import com.sundablog.pojo.AdminPermissionExample.Criteria;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.pojo.AdminUserRoleExample;
import com.sundablog.result.MenuResult;
import com.sundablog.service.index.IndexService;

/**
 * 后台接口实现类
 * @ClassName:  IndexServiceImpl   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 哒哒 
 * @date:   2018年2月26日 下午10:22:21   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class IndexServiceImpl implements IndexService {

	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;
	
	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;
	
	@Autowired
	private AdminPermissionMapper adminPermissionMapper;
	
	/**
	 * 根据用户id获取菜单
	 * Title: getMenuResult   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.index.IndexService#getMenuResult(java.lang.Integer)
	 */
	@Override
	public List<MenuResult> getMenuResult(Integer userId) {

		// 根据用户获取角色ID
		AdminUserRoleExample example = new AdminUserRoleExample();
		com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<AdminUserRole> list = adminUserRoleMapper.selectByExample(example);
		AdminUserRole userRole = list.get(0);

		// 根据角色id 获取权限列表id
		AdminRolePermissionExample example2 = new AdminRolePermissionExample();
		com.sundablog.pojo.AdminRolePermissionExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andRoleIdEqualTo(userRole.getRoleId());
		List<AdminRolePermission> list2 = adminRolePermissionMapper.selectByExample(example2);

		List<MenuResult> menuResults = new ArrayList<>();

		for (AdminRolePermission adminRolePermission : list2) {
			// 根据权限id 查询权限数据
			AdminPermission adminPermission = adminPermissionMapper
					.selectByPrimaryKey(adminRolePermission.getPermissionId());
			if (adminPermission.getType() != 3) {
				MenuResult menuResult = new MenuResult();
				menuResult.setId(adminPermission.getPermissionId().toString());
				menuResult.setPid(adminPermission.getPid().toString());
				menuResult.setTitle(adminPermission.getName());
				menuResult.setIcon(adminPermission.getIcon());
				menuResult.setUrl(adminPermission.getUri());
				menuResults.add(menuResult);
			}
		}
		List<MenuResult> trees = new ArrayList<MenuResult>(); 
		for (MenuResult menuResult : menuResults) {
			if ("0".equals(menuResult.getPid())) {  
                trees.add(findChildren(menuResult,menuResults));  
            }  
		}
		return trees;
	}

	/**
	 * 递归查找子节点
	 * 
	 * @param treeNodes
	 * @return
	 */
	public static MenuResult findChildren(MenuResult treeNode, List<MenuResult> treeNodes) {
		for (MenuResult it : treeNodes) {
			if (treeNode.getId().equals(it.getPid())) {
				if (treeNode.getChildren() == null) {
					treeNode.setChildren(new ArrayList<MenuResult>());
				}
				treeNode.getChildren().add(findChildren(it, treeNodes));
			}
		}
		return treeNode;
	}

}
